package as3.component.controls
{
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * 翻页组件(视图和位置自己添加，只实现逻辑)
	 * @author 周玉龙
	 * 二〇一一年八月二十八日 16:40:49
	 */	
	public class TurnPageComponent extends Sprite
	{
	
		//--------------------------------------------------------------------------
		//
		//  variable
		//
		//--------------------------------------------------------------------------
		/**翻页*/
		public static const PAGE_TURNING:String = "page_turning";
		
		private var simpleButton1:SimpleButton;
		
		private var simpleButton2:SimpleButton;
		
		private var upState1:DisplayObject;
		
		private var upState2:DisplayObject;
		
		private var un1:DisplayObject;
		
		private var un2:DisplayObject;
		
		public function TurnPageComponent()
		{
			init();
		}
	
		//--------------------------------------------------------------------------
		//
		//  property
		//
		//--------------------------------------------------------------------------
		private var _current:uint;

		/**当前页*/
		public function get current():uint
		{
			return _current;
		}

		/**
		 * @private
		 */
		public function set current(value:uint):void
		{
			_current = value;
		}
		
		private var _total:uint;

		/**总共页*/
		public function get total():uint
		{
			return _total;
		}

		/**
		 * @private
		 */
		public function set total(value:uint):void
		{
			_total = value;
		}
		
		//--------------------------------------------------------------------------
		//
		//  public
		//
		//--------------------------------------------------------------------------
		/**
		 * 设置上或者左状态图片(如果没有那么多图片，就用同一个资源即可)
		 * @param unOperate 不能操作状态
		 */		
		public function setUpIcon(upState:DisplayObject,overState:DisplayObject,downState:DisplayObject,unOperate:DisplayObject):void
		{
			simpleButton1.upState = upState;
			simpleButton1.overState = overState;
			simpleButton1.downState = downState;
			simpleButton1.hitTestState = overState;
			simpleButton1.width = upState.width + 2;
			un1 = unOperate;
			upState1 = upState;
			this.addChild(simpleButton1);
			simpleButton1.addEventListener(MouseEvent.CLICK,eventHandler);
		}
		
		/**
		 * 设置下或者右状态图片(如果没有那么多图片，就用同一个资源即可)
		 * @param unOperate 不能操作状态
		 */		
		public function setDownIcon(upState:DisplayObject,overState:DisplayObject,downState:DisplayObject,unOperate:DisplayObject):void
		{
			simpleButton2.upState = upState;
			simpleButton2.overState = overState;
			simpleButton2.downState = downState;
			simpleButton2.hitTestState = overState;
			simpleButton2.width = upState.width + 2;
			un2 = unOperate;
			upState2 = upState;
			this.addChild(simpleButton2);
			simpleButton2.addEventListener(MouseEvent.CLICK,eventHandler);
		}
		
		
		public function setPosition(upX1:Number,downY1:Number,upX2:Number,downY2:Number):void
		{
			simpleButton1.x = upX1;
			simpleButton1.y = downY1;
			simpleButton2.x = upX2;
			simpleButton2.y = downY2;
		}
		
		/**
		 * 控制箭头状态
		 */		
		public function setValue(_current:uint,_total:uint):void
		{
			current = _current;
			total = _total;
			if(total == 1)//只有一页
			{
				simpleButton1.upState = un1;
				simpleButton1.mouseEnabled = false;
				simpleButton2.upState = un2;
				simpleButton2.mouseEnabled = false;
			}
			else if(current == 1)//当前为第一页
			{
				simpleButton1.upState = un1;
				simpleButton1.mouseEnabled = false;
				simpleButton2.upState = upState2;
				simpleButton2.mouseEnabled = true;
			}
			else if(current == total)//当前页为最后一页
			{
				simpleButton1.upState = upState1;
				simpleButton1.mouseEnabled = true;
				simpleButton2.upState = un2;
				simpleButton2.mouseEnabled = false;
			}
			else//中间页
			{
				if(simpleButton1.mouseEnabled == false)
				{
					simpleButton1.upState = upState1;
					simpleButton1.mouseEnabled = true;
				}
				if(simpleButton2.mouseEnabled == false)
				{
					simpleButton2.upState = upState2;
					simpleButton2.mouseEnabled = true;
				}
			}
		}
		//--------------------------------------------------------------------------
		//
		//  private
		//
		//--------------------------------------------------------------------------
		private function init():void
		{
			simpleButton1 = new SimpleButton();
			simpleButton2 = new SimpleButton();
		}
		//--------------------------------------------------------------------------
		//
		//  event
		//
		//--------------------------------------------------------------------------
		private function eventHandler(e:MouseEvent):void
		{
			switch(e.currentTarget)
			{
				case simpleButton1:
					setValue(--current,total);
					break;
				case simpleButton2:
					setValue(++current,total);
					break;
			}
			this.dispatchEvent(new Event(PAGE_TURNING));
		}
	}
}